package com.thug.states;

import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.Scene.IOnSceneTouchListener;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.input.touch.TouchEvent;

import android.util.Log;

import com.thug.ai.GamePlayAI;
import com.thug.ai.StageMap;
import com.thug.engine.EntityManager;
import com.thug.engine.EntityManagers;
import com.thug.engine.MyContactListener;
import com.thug.engine.SceneManager;
import com.thug.engine.SceneState;
import com.thug.engine.StageLoader;
import com.thug.engine.StageSettings;
import com.thug.engine.ZoomCameraController;
import com.thug.entities.Bot;
import com.thug.managers.BotManager;
import com.thug.managers.PlayerManager;

public class GamePlayState extends SceneState implements IOnSceneTouchListener {

	private PlayerManager mPlayerManager;
	private StageMap mStageMap;
	private ZoomCameraController mCameraController;
	private StageSettings mStageSettings;
	private GamePlayAI mGamePlayAI;
	private EntityManagers mManagers;
	private EntityManager<Bot> mBotManager;
	private GameHud mGameHud;

	public GamePlayState(SceneManager s) {
		super(s);
		registerUpdateHandler(getSceneMgr().getPhysicsWorld());
		setOnSceneTouchListener(this);
		getSceneMgr().getPhysicsWorld().setContactListener(new MyContactListener(getSceneMgr()));
		mPlayerManager = new PlayerManager(getSceneMgr());
		mManagers = EntityManagers.getInstance();
		mGamePlayAI = GamePlayAI.getInstance();
	}

	@Override
	public void onLoad() {
		setBackground(new ColorBackground(0.04f, 0.04f, 0.04f));
		// test code
		StageLoader loader =  new StageLoader(getSceneMgr());
		loader.LoadStage("theif.txt");
		mStageSettings = (StageSettings) getSceneMgr().getResource("stage_settings");
		//
		mManagers.onLoadManagers();
		mPlayerManager.onLoad();
		//mBotManager = mManagers.getEntityManager(BotManager.class);
		mBotManager = (BotManager) getSceneMgr().getResource("bot_mgr");
		if(mBotManager == null) {
			Log.d("Follow Player State", "bot entity manager not found");
		}
		mCameraController = new ZoomCameraController(getSceneMgr().getCamera(), mPlayerManager.getPlayer());
		mCameraController.setBounds(mStageSettings.getStartX(), mStageSettings.getEndX(), mStageSettings.getStartY(), mStageSettings.getEndY());
		mCameraController.setBoundsEnabled(true);
		mStageMap = new StageMap(getSceneMgr(), mStageSettings.getStartX(), mStageSettings.getStartY(), mStageSettings.getEndX(), mStageSettings.getEndY(), mStageSettings.getColumns(), mStageSettings.getRows(), 1);
		mStageMap.updateWalkableAreas();
		/* register to AI */
		mGamePlayAI.registerSceneManager(getSceneMgr());
		mGamePlayAI.registerStageMap(mStageMap);
		mGamePlayAI.registerPlayer(mPlayerManager.getPlayer());
		mGamePlayAI.registerEntities(mBotManager.getEntities());
		/* setup hud */
		mGameHud = new GameHud(getSceneMgr());
		mGameHud.init();
		mPlayerManager.getOnScreenControl().setChildScene(mGameHud);
		getSceneMgr().getCamera().setHUD(mGameHud);
		
		mGameHud.show(true, true, true, false, false);
	}

	@Override
	public void onDestroy() {
		mGamePlayAI.unRegisterEntities(mBotManager.getEntities());
		mManagers.onDestroyManagers();
		mPlayerManager.getOnScreenControl().clearChildScene();
		mGameHud.clearChildScene();
		mGameHud.clearTouchAreas();
		mGameHud.detachChildren();
		mGameHud.detachSelf();
		this.clearChildScene();
		this.detachChildren();
		//getSceneMgr().getCamera().setHUD(null);
	}


	@Override
	protected void onManagedUpdate(float pSecondsElapsed) {
		mPlayerManager.update(pSecondsElapsed);
		mCameraController.update(pSecondsElapsed);
		mManagers.updateManagers(pSecondsElapsed);
		mGamePlayAI.update(pSecondsElapsed);
		checkForBtns();
		super.onManagedUpdate(pSecondsElapsed);
	}

	@Override
	public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {

		return false;
	}
	
	private void checkForBtns() {
		if(mGameHud.isBackBtnPressed()) {
			//getSceneMgr().changeState(new MainMenuState(getSceneMgr()));
			getSceneMgr().popState();
		}
		
		if(mGameHud.isZoomInPressed()) {
			mCameraController.btnZoomIn();
		}
		
		if(mGameHud.isZoomOutPressed()) {
			mCameraController.btnZoomOut();
		}
		
		mGameHud.makeFalse();
	}

	
}
